/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.upn.sigemfi.vfi.bc;

import edu.upn.sigemfi.vfi.be.PagoCredito;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author Programador
 */
public class PagoCreditoDelegate {


    private PagoCreditoFacadeLocal local;

    public PagoCreditoDelegate() {
        local = lookupPagoCreditoFacade();
    }

    public void crear(PagoCredito pagoCredito) throws Exception {
        try {
            local.create(pagoCredito);
        } catch (Exception e) {
            local = lookupPagoCreditoFacade();
            throw new Exception("Crear PagoCredito. " + e.getMessage(), e);
        }
    }

    public void editar(PagoCredito pagoCredito) throws Exception {
        try {
            local.edit(pagoCredito);
        } catch (Exception e) {
            local = lookupPagoCreditoFacade();
            throw new Exception("Editar PagoCredito. " + e.getMessage(), e);
        }
    }

    public void eliminar(PagoCredito pagoCredito) throws Exception {
        try {
            local.remove(pagoCredito);
        } catch (Exception e) {
            local = lookupPagoCreditoFacade();
            throw new Exception("Eliminar PagoCredito. " + e.getMessage(), e);
        }
    }

    public PagoCredito buscar(Long codigo) throws Exception {
        try {
            return local.find(codigo);
        } catch (Exception e) {
            local = lookupPagoCreditoFacade();
            throw new Exception("Buscar PagoCredito. " + e.getMessage(), e);
        }
    }

    public List<PagoCredito> listar() throws Exception {
        try {
            return local.findAll();
        } catch (Exception e) {
            local = lookupPagoCreditoFacade();
            throw new Exception("Listar PagoCredito. " + e.getMessage(), e);
        }
    }

    private PagoCreditoFacadeLocal lookupPagoCreditoFacade() {
        try {
            Context c = new InitialContext();
            return (PagoCreditoFacadeLocal) c.lookup("java:comp/env/PagoCreditoFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

}
